﻿using Loki.Commands;
using Luna.Model.Budget;
using Luna.UI.Budget;
using Luna.UI.Contacts;
using Luna.UI.CRM;

namespace Luna.UI
{
    public class OpenDocumentsViewModel : LunaContainerViewModel
    {
        public OpenDocumentsViewModel()
        {
            Init += OpenDocumentsViewModel_Init;
        }

        private void Application_OpenBudget_CanExecute(object P_Sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= e.Parameter is IBudget || e.Parameter is IEditableBudget;
        }

        private void Application_OpenBudget_Execute(object P_Sender, CommandEventArgs e)
        {
            IEditableBudget L_EditableCategory = e.Parameter as IEditableBudget;
            if (L_EditableCategory != null)
            {
                AddNew<BudgetEditViewModel>(v => v.CurrentBudget = L_EditableCategory);
            }
            else
            {
                IBudget L_Budget = e.Parameter as IBudget;
                if (L_Budget != null)
                {
                    AddNew<BudgetEditViewModel>(v => v.CurrentBudget = L_Budget.EditableCopy);
                }
            }
        }

        private void Navigation_Accounts_Execute(object P_Sender, CommandEventArgs e)
        {
            ActivateOrAdd<AccountListViewModel>();
        }

        private void Navigation_Budget_Categories_Execute(object P_Sender, CommandEventArgs e)
        {
            ActivateOrAdd<Budget.CategoryListViewModel>();
        }

        private void Navigation_Budget_List_Execute(object P_Sender, CommandEventArgs e)
        {
            ActivateOrAdd<BudgetListViewModel>();
        }

        private void Navigation_Categories_Execute(object P_Sender, CommandEventArgs e)
        {
            ActivateOrAdd<CRM.CategoryListViewModel>();
        }

        private void Navigation_Contacts_Execute(object P_Sender, CommandEventArgs e)
        {
            ActivateOrAdd<ContactListViewModel>();
        }

        private void OpenDocumentsViewModel_Init(object sender, System.EventArgs e)
        {
            Commands.Handle(NavigationCommands.CONTACTS, Navigation_Contacts_Execute);
            Commands.Handle(NavigationCommands.ACCOUNTS, Navigation_Accounts_Execute);
            Commands.Handle(NavigationCommands.CATEGORIES, Navigation_Categories_Execute);
            Commands.Handle(NavigationCommands.BUDGET_CATEGORIES, Navigation_Budget_Categories_Execute);
            Commands.Handle(NavigationCommands.BUDGET_LIST, Navigation_Budget_List_Execute);

            Commands.Handle(ApplicationCommands.OPEN, Application_OpenBudget_CanExecute, Application_OpenBudget_Execute);
        }
    }
}